home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / BPNN133U.ZIP / CPARSER.C < prev    next >
C/C++ Source or Header  |  1992-11-19  |  4KB  |  190 lines

  1. /*
  2. *-----------------------------------------------------------------------------
  3. *    file:    cparser.c
  4. *    desc:    simple command parser
  5. *    by:    patrick ko
  6. *    date:    22 aug 91 v0.1
  7. *    revi:    26 feb 92 v0.2; response file feature
  8. *    revi:    27 may 92 v0.3; fix the multiple response file bug
  9. *-----------------------------------------------------------------------------
  10. */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. #ifdef __TURBOC__
  15. #include <mem.h>
  16. #include <ctype.h>
  17. #endif
  18.  
  19. #include "cparser.h"
  20.  
  21. #define CP_START    1
  22. #define    CP_GETCMDLINE    2
  23. #define    CP_GETRSPLINE    3
  24. #define    CP_CMDSEARCH    4
  25. #define    CP_RETURN    5
  26.  
  27. CMDTBL    cmdtbl[] =
  28.     {
  29.     {CMD_DIMINPUT,        "-i="},
  30.     {CMD_DIMOUTPUT,        "-o="},
  31.     {CMD_DIMHIDDENY,    "-hh="},
  32.     {CMD_DIMHIDDEN,        "-h="},
  33.     {CMD_TRAINFILE,        "-ftrain="},
  34.     {CMD_TOTALPATT,        "-samp="},
  35.     {CMD_DUMPIN,        "-fdumpin="},
  36.     {CMD_DUMPFILE,        "-fdump="},
  37.     {CMD_RECOGFILE,        "-frecog="},
  38.     {CMD_OUTFILE,        "-fout="},
  39.     {CMD_TRAINERR,        "-err="},
  40.     {CMD_TOLER,        "-torerr="},
  41.     {CMD_REPORT,        "-r="},
  42.     {CMD_TDUMP,        "-tdump="},
  43.     {CMD_TIMER,        "-t"},
  44.     {CMD_WPOS,        "-w+="},
  45.     {CMD_WNEG,        "-w-="},
  46.     {CMD_COMMENT,        "//"}
  47.     };
  48.  
  49. static int    cmdtblsize = 0;
  50. static int    cmdargc = 0;
  51. static int    cmdcnt = 0;
  52. static char    **cmdargv;
  53.  
  54.  
  55. int cmdsearch( str, rest )
  56. char    *str;
  57. char    *rest;
  58. {
  59.     int    i, l;
  60.     
  61.     for (i=0; i<cmdtblsize; i++)
  62.         {
  63.         l = strlen( cmdtbl[i].cmdstr );
  64.         if (!memcmp(str, cmdtbl[i].cmdstr, l))
  65.             {
  66.             strcpy( rest, str + l );
  67.             return (cmdtbl[i].cmdno);
  68.             }
  69.         }
  70.     strcpy( rest, str );
  71.     return (CMD_NULL);
  72. }
  73.  
  74. int cmdinit( argc, argv )
  75. int    argc;
  76. char    **argv;
  77. {
  78.     cmdtblsize = sizeof(cmdtbl) / sizeof(cmdtbl[0]);
  79.     cmdargc = argc;
  80.     cmdargv = argv;
  81. }
  82.  
  83.  
  84. /*
  85. *------------------------------------------------------------------------------
  86. *    funct:    cmdget
  87. *    desct:    get a command either from command line or response file
  88. *    given:    rest = rest of the command line string
  89. *        e.g. if a command is "-example=" and in the command line
  90. *        we have "-example=32787" then rest will contain "32787".
  91. *    retrn:    the command token specified in cparser.h (you defined)
  92. *
  93. *    state table:
  94. *
  95. *        START  GETCMD  GETRSP  SEARCH  RETURN
  96. *    START    x      fsp=0   fsp!=0  x       x
  97. *    GETCMD    x      x       @       not @   no cmd 
  98. *    GETRSP    no cmd x       x       cmd     x
  99. *    SEARCH  x      x       x       x       result
  100. *    RETURN  x      x       x       x       x
  101. *------------------------------------------------------------------------------
  102. */
  103. int cmdget( rest )
  104. char     *rest;
  105. {
  106.     int    i, j;
  107.     int    state, result;
  108.     char    cmdstr[129], *rspname;
  109.     static FILE *frsp;
  110.  
  111.     state = CP_START;
  112.     result = -1;
  113.     *cmdstr = 0;
  114.  
  115.     while (1)
  116.         {
  117.         switch (state)
  118.         {
  119.         case CP_START:
  120.         if (frsp == NULL)
  121.             state = CP_GETCMDLINE;
  122.         else
  123.             state = CP_GETRSPLINE;
  124.         break;
  125.  
  126.         case CP_GETCMDLINE:
  127.         if ((cmdcnt >= cmdargc - 1))
  128.             {
  129.             state = CP_RETURN;
  130.             result = -1;
  131.             }
  132.         else
  133.             {
  134.             /* test for response file */
  135.             rspname = *(cmdargv + cmdcnt + 1);
  136.             cmdcnt++;
  137.             if (*rspname == '@')
  138.             {
  139.             rspname++;
  140.                 if ((frsp = fopen(rspname, "r")) == NULL)
  141.                 {
  142.                 fprintf(stderr, "rsp file open fails\n");
  143.                 exit (1);
  144.                 }
  145.                 else
  146.                 state = CP_GETRSPLINE;
  147.                 }
  148.             else
  149.             {
  150.                 strcpy( cmdstr, *(cmdargv + cmdcnt) );
  151.                 state = CP_CMDSEARCH;    
  152.             }
  153.             }
  154.         break;
  155.  
  156.         case CP_GETRSPLINE:
  157.         for (;!strlen(cmdstr) && !feof(frsp);)
  158.             {
  159.             fgets(cmdstr, 128, frsp);
  160.             for (i=j=0; i<strlen(cmdstr); i++)
  161.                 {
  162.                 if (!isspace(cmdstr[i]))
  163.                     {
  164.                     cmdstr[j++] = cmdstr[i];
  165.                     }
  166.                 }
  167.             cmdstr[j] = 0;
  168.             }
  169.         if (feof(frsp))
  170.             {
  171.             fclose(frsp);
  172.             frsp = NULL;
  173.             }
  174.         if (strlen(cmdstr))
  175.             state = CP_CMDSEARCH;
  176.         else 
  177.             state = CP_START;
  178.         break;
  179.     
  180.         case CP_CMDSEARCH:
  181.         result = cmdsearch( cmdstr, rest );
  182.         state = CP_RETURN;
  183.         break;
  184.  
  185.         case CP_RETURN:
  186.         return (result);
  187.         }
  188.         }
  189. }
  190.